﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RuoVea.ExCaptcha;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace RuoVea.WebTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;


        private readonly IClickWordCaptcha _captchaHandle;// 验证码服务
        private readonly IGeneralCaptcha _gneralCaptcha;// 验证码服务

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IClickWordCaptcha captchaHandle, IGeneralCaptcha gneralCaptcha)
        {
            _logger = logger;
            _captchaHandle = captchaHandle;
            _gneralCaptcha = gneralCaptcha;
        }

        /// <summary>
        /// 获取验证码（默认点选模式）
        /// </summary>
        /// <returns></returns>
        [HttpPost("/clickcaptcha/get")]
        [AllowAnonymous]
        //[NonUnify]
        public async Task<dynamic> GetClickCaptcha()
        {
            RuoVea.ExConfig.Appsettings.GetSection("");
            // 图片大小要与前端保持一致（坐标范围）
            var b = _captchaHandle.CreateCaptchaImage(_captchaHandle.RandomCode(6), 310, 155);
            return await Task.FromResult(b);
        }

        /// <summary>
        /// 校验验证码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/clickcaptcha/check")]
        [AllowAnonymous]
        //[NonUnify]
        public async Task<dynamic> ClickCaptchaCheck(string token, string captchaCode)
        {
            return await Task.FromResult(_captchaHandle.CheckCode(token, captchaCode));
        }



        [HttpPost("/generalcaptcha/get")]
        [AllowAnonymous]
        //[NonUnify]
        public async Task<dynamic> GetGeneralCaptcha()
        {
            // 图片大小要与前端保持一致（坐标范围）
            var b = _gneralCaptcha.CreateCaptchaImage(4);
            return await Task.FromResult(b);
        }


        [HttpPost("/generalcaptcha/check")]
        [AllowAnonymous]
        //[NonUnify]
        public async Task<dynamic> GeneralCaptchaCheck(string token, string captchaCode)
        {
            return await Task.FromResult(_gneralCaptcha.CheckCode(token, captchaCode));
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {

            //var allowedHosts = Appsettings.app("AllowedHosts");
            //Console.WriteLine("AllowedHosts:" + allowedHosts);

            //var cacheType = ExCache.CacheOptions._cacheType;
            //Debug.WriteLine("cacheType:" + cacheType.ToString());

            //var prefix = ExCache.CacheOptions._prefix;
            //Debug.WriteLine("prefix:" + prefix);

            //var redisConnectionString = ExCache.CacheOptions._redisConnectionString;
            //Debug.WriteLine("redisConnectionString:" + redisConnectionString);

            //ExCache.CacheOptions.CaChe.Write("asdd", "assadasdasdasdasdasas");
            //var getValue = ExCache.CacheOptions.CaChe.Read("asdd");

            ////    IMemoryCache _memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            ////_memoryCache.Set("asd", "asdasd");
            //Debug.WriteLine("getValue:" + getValue);

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}
